#include "CEUtilities.h"

#include "CEColor.h"
#include "CEFont.h"
#include "CEGUIManager.h"
#include "CEGUIPictureBox.h"
#include "CEGUILabel.h"
#include "CEGUIButtonBase.h"


CEGUIButtonBase::ButtonState CEGUIButtonBase::Str2ButtonState(const string& str)
{
	if (str == "BTS_NORMAL")
		return BTS_NORMAL;
	else if (str == "BTS_MOUSEOVER")
		return BTS_MOUSEOVER;
	else if (str == "BTS_BUTTONDOWN")
		return BTS_BUTTONDOWN;
	else if (str == "BTS_DISABLE")
		return BTS_DISABLE;
	else 
		return BTS_NORMAL;
}

string CEGUIButtonBase::ButtonState2Str(const ButtonState& state)
{
	if (state == BTS_NORMAL)
		return "BTS_NORMAL";
	else if (state == BTS_MOUSEOVER)
		return "BTS_MOUSEOVER";
	else if (state == BTS_BUTTONDOWN)
		return "BTS_BUTTONDOWN";
	else if (state == BTS_DISABLE)
		return "BTS_DISABLE";
	else 
		return "BTS_NORMAL";
}



CEGUIButtonBase::CEGUIButtonBase(CEGUIElementBase * parent, int myid, string myname, 
		string imageNormal, string imageOver, string imageButtonDown, string imageDisabled, 
		string strText, int offsetX, int offsetY, int width, int height, 
		 bool visible, 	int tabindex, bool tabenable) 
		: CEGUIElementBase(parent, myid, myname, offsetX, offsetY, 
		width, height, true, visible, tabindex, tabenable)
		, m_pLabel(NULL)
{
	assert(!imageNormal.empty());

	int id = CEGUIManager::GetSingleton()->GetNextID();
	string name = CEUtil::IntToStr(id);
	m_pPicNormal = new CEGUIPictureBox(this, id, name, imageNormal, true, Color::White, 0, 0, width, height);

	if (!imageOver.empty())
	{
		id = CEGUIManager::GetSingleton()->GetNextID();
		name = CEUtil::IntToStr(id);
		m_pPicMouseOver = new CEGUIPictureBox(this, id, name, imageOver, true, Color::White, 0, 0, width, height);
	}
	else
		m_pPicMouseOver = NULL;

	if (!imageButtonDown.empty())
	{
		id = CEGUIManager::GetSingleton()->GetNextID();
		name = CEUtil::IntToStr(id);
		m_pPicButtonDown = new CEGUIPictureBox(this, id, name, imageButtonDown, true, Color::White, 0, 0, width, height);
	}
	else
		m_pPicButtonDown = NULL;

	if (!imageDisabled.empty())
	{
		id = CEGUIManager::GetSingleton()->GetNextID();
		name = CEUtil::IntToStr(id);
		m_pPicButtonDisable = new CEGUIPictureBox(this, id, name, imageDisabled, true, Color::White, 0, 0, width, height);
	}
	else
		m_pPicButtonDisable = NULL;
	
	SetLabelText(strText);

	m_ButtonState = BTS_NORMAL;
}


CEGUIButtonBase::~CEGUIButtonBase()
{
	FinalCleanUp();
}

void CEGUIButtonBase::SetPicture(const ButtonState& state, const string& image)
{
	CEGUIPictureBox* pic = NULL;
	switch(state) {
	case BTS_NORMAL:
		pic = m_pPicNormal;
		break;
	case BTS_MOUSEOVER:
		pic = m_pPicMouseOver;
		break;
	case BTS_DISABLE:
		pic = m_pPicButtonDisable;
		break;
	case BTS_BUTTONDOWN:
		pic = m_pPicButtonDown;
		break;
	default:
		return;
	}

	if (pic)
		pic->ResetImage(image, Color::White);
	else
	{
		int id = CEGUIManager::GetSingleton()->GetNextID();
		string name = CEUtil::IntToStr(id);
		pic = new CEGUIPictureBox(this, id, name, image, true, Color::White);
	}
}



CEGUIPictureBox* CEGUIButtonBase::GetPiciure(const ButtonState& state) const
{
	switch(state) {
	case BTS_NORMAL:
		return m_pPicNormal;
		break;
	case BTS_MOUSEOVER:
		return m_pPicMouseOver;
		break;
	case BTS_DISABLE:
		return m_pPicButtonDisable;
		break;
	case BTS_BUTTONDOWN:
		return m_pPicButtonDown;
		break;
	default:
		return NULL;
	}
}


CEGUIPictureBox* CEGUIButtonBase::GetPicture() const
{
	switch(m_ButtonState) {
	case BTS_NORMAL:
		return m_pPicNormal;
		break;
	case BTS_MOUSEOVER:
		if (m_pPicMouseOver) return m_pPicMouseOver;
		else return m_pPicNormal;
		break;
	case BTS_DISABLE:
		if (m_pPicButtonDisable) return m_pPicButtonDisable;
		else return m_pPicNormal;
		break;
	case BTS_BUTTONDOWN:
		if (m_pPicButtonDown) return m_pPicButtonDown;
		else return m_pPicNormal;
		break;
	default:
		return NULL;
	}
}


void CEGUIButtonBase::SetPosition(int offestfromparentx, int offestfromparenty)
{
	CEGUIElementBase::SetPosition(offestfromparentx, offestfromparenty);
	m_pPicNormal->Move(0,0);
	if (m_pPicButtonDown)
		m_pPicButtonDown->Move(0,0);
	if (m_pPicButtonDisable)
		m_pPicButtonDisable->Move(0,0);
	if (m_pPicMouseOver)
		m_pPicMouseOver->Move(0,0);

	if(m_pLabel)
		m_pLabel->Move(0,0);
}


void CEGUIButtonBase::SetSize(int width, int height)
{
	CEGUIElementBase::SetSize(width,height);
	m_pPicNormal->SetSize(width,height);
	if (m_pPicButtonDown)
		m_pPicButtonDown->SetSize(width,height);
	if (m_pPicButtonDisable)
		m_pPicButtonDisable->SetSize(width,height);
	if (m_pPicMouseOver)
		m_pPicMouseOver->SetSize(width,height);

	if(m_pLabel)
		m_pLabel->SetSize(width,height);
}


void CEGUIButtonBase::Move(int offsetfromparentx, int offsetfromparenty)
{
	CEGUIElementBase::Move(offsetfromparentx,offsetfromparenty);
	m_pPicNormal->Move(0,0);
	if (m_pPicButtonDown)
		m_pPicButtonDown->Move(0,0);
	if (m_pPicButtonDisable)
		m_pPicButtonDisable->Move(0,0);
	if (m_pPicMouseOver)
		m_pPicMouseOver->Move(0,0);

	if(m_pLabel)
		m_pLabel->Move(0,0);
}


HRESULT CEGUIButtonBase::Render()
{
	if(m_bVisible)
	{
		HRESULT r = GetPicture()->Render();
		if (m_pLabel != NULL)
			r |= m_pLabel->Render();
		return r;
	}
	else
		return S_FALSE;
}

void CEGUIButtonBase::FinalCleanUp()
{
	SAFE_DELETE(m_pPicNormal);
	SAFE_DELETE(m_pPicButtonDown);
	SAFE_DELETE(m_pPicButtonDisable);
	SAFE_DELETE(m_pPicMouseOver);
	SAFE_DELETE(m_pLabel);
}


void CEGUIButtonBase::SetLabelText(const string& text)
{
	if (!text.empty())
	{
		if (m_pLabel != NULL)
		{
			m_pLabel->SetText(text);
		}
		else
		{
			int id = CEGUIManager::GetSingleton()->GetNextID();
			string name = CEUtil::IntToStr(id);
			m_pLabel = new CEGUILabel(this, id, name, text, 0, 0, m_Size.x, m_Size.y);
			SetLabelAlignMode(AF_VMD_CTR);
		}
	}
	else
	{
		SAFE_DELETE(m_pLabel);

	}
}

void CEGUIButtonBase::SetLabelTextColor(const Color& colorkey)
{
	if(m_pLabel)
		m_pLabel->SetTextColor(colorkey);	
}


void CEGUIButtonBase::SetLabelPosition(int lefttopx, int lefttopy)
{
	if(m_pLabel)
		m_pLabel->SetPosition(lefttopx, lefttopy);
}

void CEGUIButtonBase::SetLabelSize(int width, int height)
{
	
	if(m_pLabel)
		m_pLabel->SetSize(width, height);
}


void CEGUIButtonBase::SetLabelAlignMode(const ALIGN_FORMAT& format)
{
	if(m_pLabel)
		m_pLabel->SetFormat(format);
}



void CEGUIButtonBase::SetPicture(const ButtonState& state, CEGUIPictureBox *pic)
{
	switch(state) {
	case BTS_NORMAL:
		SAFE_DELETE(m_pPicNormal);
		m_pPicNormal = pic;
		break;
	case BTS_MOUSEOVER:
		SAFE_DELETE(m_pPicMouseOver);
		m_pPicMouseOver = pic;
		break;
	case BTS_BUTTONDOWN:
		SAFE_DELETE(m_pPicButtonDown);
		m_pPicButtonDown = pic;
		break;
	case BTS_DISABLE:
		SAFE_DELETE(m_pPicButtonDisable);
		m_pPicButtonDisable = pic;
		break;
	}
	pic->SetPosition(0, 0);
	pic->SetSize(GetSize().x, GetSize().y);
}


void CEGUIButtonBase::SetPictures(const string& image, unsigned int imageLength, unsigned int imageHeight,
				unsigned int btnLength, unsigned int btnHeight)
				
{
	SetPicture(BTS_NORMAL, image);
	SetPicture(BTS_MOUSEOVER, image);
	SetPicture(BTS_BUTTONDOWN, image);
	SetPicture(BTS_DISABLE, image);

	m_pPicNormal->SetUVMap(0,static_cast<float>(btnHeight)/imageHeight * 0,static_cast<float>(btnLength)/imageLength,static_cast<float>(btnHeight)/imageHeight * 1);
	m_pPicMouseOver->SetUVMap(0,static_cast<float>(btnHeight)/imageHeight * 1,static_cast<float>(btnLength)/imageLength,static_cast<float>(btnHeight)/imageHeight * 2);
	m_pPicButtonDown->SetUVMap(0,static_cast<float>(btnHeight)/imageHeight * 2,static_cast<float>(btnLength)/imageLength,static_cast<float>(btnHeight)/imageHeight * 3);
	m_pPicButtonDisable->SetUVMap(0,static_cast<float>(btnHeight)/imageHeight * 3,static_cast<float>(btnLength)/imageLength,static_cast<float>(btnHeight)/imageHeight * 4);
}


void CEGUIButtonBase::SetLabel(CEGUILabel * label)
{
	SAFE_DELETE(m_pLabel);
	m_pLabel = label;
}